{% extends "./test_base.html" %}
{% load static %}

{% block specific_page_css %}
<style>
	body button.button_group1+div+div {
      width: 700px;
      margin-top: 7px;
      margin-left: 1em;
      z-index: 1000;
    }
</style>
{% endblock %}

{% block title %}
<title>分析结果管理 QC信息 甲基化检测</title>
{% endblock %}

{% block header_title %}
<h1>QC信息 甲基化检测</h1>
{% endblock %}

{% block header_ol %}
	<li class="breadcrumb-item"><a href="/Home/">Home</a></li>
	<li class="breadcrumb-item">SEQ</li>
	<li class="breadcrumb-item active">MethyQCInfo</li>
{% endblock %}

{% block card-body %}
<table id="table" class="table table-bordered table-hover dt-responsive">
<thead>
<tr id="table_header">

</tr>
</thead>
</table>
{% endblock %}

{% block page-script %}
<script>
  $(document).ready(function () {
      $("#sidebar_SEQ>a").addClass("active");
	  $("#sidebar_SEQ").addClass("menu-open");
	  $("#sidebar_SEQ>ul>li:eq(1)>a").addClass("active");
	  $("#sidebar_SEQ>ul>li:eq(1)>ul>li:eq(0)>a").addClass("active");
	  $(".sidebar").removeAttr("hidden");

	  let model0 = "MethyQCInfo";
	  let url0 = '/api/SEQ/QC/' + model0 + '/';
	  let url1 = '/api/LIMS/Methy/MethyPoolingInfo/';
	  let url2 = '/api/SEQ/SequencingInfo/';
	  let id0 = 0;
      let bulk_update_bool = {% if perms.BIS.bulk_update_MethyQCInfo %} true {% else %} false {% endif %};
      let bulk_delete_bool = {% if perms.BIS.bulk_delete_MethyQCInfo %} true {% else %} false {% endif %};

      function update_func(e){
          e.preventDefault();
		    // 递交前检查，一般是检测model的外键是否存在
			let data1 = $.ajax({
				url: url1, dataType: 'json', contentType: "application/json",
				type: "get", async: false, data: { singleLB_Pooling_id: $('#singleLB_Pooling_id').val(), fields: "id,sampleinventoryinfo" }
			}).responseJSON[0];
			if (data1 === undefined) {
				$('#myModalError').text('错误！！！测序文库编号不存在，请填写正确的测序文库编号。');
				return
			}
			let data2 = $.ajax({
				url: url2, dataType: 'json', contentType: "application/json",
				type: "get", async: false, data: { sequencing_id: $('#sequencing_id').val(), fields: "id" }
			}).responseJSON[0];
			if (data2 === undefined) {
				$('#myModalError').text('错误！！！上机文库号不存在，请填写正确的上机文库号。');
				return
			}

            let type = $('#type').val();
            let method = '';
            let url_ajax0 = url0;
            let send_data0 = $("#modelForm").find(":not(#singleLB_Pooling_id, #sequencing_id)").serialize() + '&' + $.param({
				methypoolinginfo: data1.id, sequencing_id: data2.id, sampleinventoryinfo: data1.sampleinventoryinfo
			});
            if (type === 'new') {
                // new
                method = 'POST';
              // POST前检查，一般是检测model的关键字段值是否存在
                data0 = $.ajax({
                    url: url0, dataType: 'json', contentType: "application/json",
                    type: "get", async: false, data: { sequencing_id: $('#sequencing_id').val(), fields: "id" }
                }).responseJSON[0];
                if (data0 !== undefined) {
                    $('#myModalError').text('错误！！！Sample已存在，无法添加。如需更新该条目，请在对应行进行更新操作。');
                    return
                }
            } else {
                // edit
                url_ajax0 = url0 + id0 + '/';
                method = 'PUT';
            }

            $.ajax({
                url: url_ajax0,
                method: method,
                data: send_data0,
                headers: {'X-HTTP-Method-Override': 'PATCH'},
                success: function () {
                    let v_tmp = $('#poolingLB_id').val();
                    if (type === 'new') {
                      databaseRecordAjaxPut('{{ user.id }}', model0, "单项添加", "上机文库号：" + v_tmp);
                      $('#myModalError').text('添加成功！！！Sample：' + v_tmp);
                  } else {
                      databaseRecordAjaxPut('{{ user.id }}', model0, "单项更新", "上机文库号：" + v_tmp);
                      $('#myModalError').text('更新成功！！！Sample：' + v_tmp);
                  }
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    $('#myModalError').text('出现错误！！！请联系管理员');
                    console.info(url_ajax0, method, send_data0);
                    console.log(jqXHR + ';' + textStatus + ';' + errorThrown)
                }
            });
      }

      let input_params = {
          "title1_text": "分析结果管理 QC信息 甲基化检测 总条目数：",
		  "fields": [
              // input类型是select： [ field, name, 是否出现在modal, modal中排序, 是否必填, input类型, [[value, text]] ]
              // input类型不是select： [ field, name, 是否出现在modal, modal中排序, 是否必填, input类型, value ]
              ["Sample", "Sample", 1, 2, 1, "text"],
			  ["data_size_gb_field", "Data_Size(Gb)", 1, 3, 0, "number_0.001", 0],
			  ["clean_rate_field", "Clean_Rate(%)", 1, 4, 0, "number_0.001", 0],
			  ["r1_q20", "R1_Q20", 1, 5, 0, "number_0.0001", 0],
			  ["r2_q20", "R2_Q20", 1, 6, 0, "number_0.0001", 0],
			  ["r1_q30", "R1_Q30", 1, 7, 0, "number_0.0001", 0],
			  ["r2_q30", "R2_Q30", 1, 8, 0, "number_0.0001", 0],
			  ["gc_content", "GC_Content", 1, 9, 0, "number_0.0001", 0],
			  ["bs_conversion_rate_lambda_dna_field", "BS_conversion_rate(lambda_DNA)", 1, 10, 0, "number_0.0001", 0],
			  ["bs_conversion_rate_chh_field", "BS_conversion_rate(CHH)", 1, 11, 0, "number_0.0001", 0],
			  ["bs_conversion_rate_chg_field", "BS_conversion_rate(CHG)", 1, 12, 0, "number_0.0001", 0],
			  ["uniquely_paired_mapping_rate", "Uniquely_Paired_Mapping_Rate", 1, 13, 0, "number_0.0001", 0],
			  ["mismatch_and_indel_rate", "Mismatch_and_InDel_Rate", 1, 14, 0, "number_0.0001", 0],
			  ["mode_fragment_length_bp_field", "Mode_Fragment_Length(bp)", 1, 15, 0, "number_0.0001", 0],
			  ["genome_duplication_rate", "Genome_Duplication_Rate", 1, 16, 0, "number_0.0001", 0],
			  ["genome_depth_x_field", "Genome_Depth(X)", 1, 17, 0, "number_0.001", 0],
			  ["genome_dedupped_depth_x_field", "Genome_Dedupped_Depth(X)", 1, 18, 0, "number_0.001", 0],
			  ["genome_coverage", "Genome_Coverage", 1, 19, 0, "number_0.0001", 0],
			  ["genome_4x_cpg_depth_x_field", "Genome_4X_CpG_Depth(X)", 1, 20, 0, "number_0.001", 0],
			  ["genome_4x_cpg_coverage", "Genome_4X_CpG_Coverage", 1, 21, 0, "number_0.0001", 0],
			  ["genome_4x_cpg_methylation_level", "Genome_4X_CpG_methylation_level", 1, 22, 0, "number_0.0001", 0],
			  ["panel_4x_cpg_depth_x_field", "Panel_4X_CpG_Depth(X)", 1, 23, 0, "number_0.001", 0],
			  ["panel_4x_cpg_coverage", "Panel_4X_CpG_Coverage", 1, 24, 0, "number_0.0001", 0],
			  ["panel_4x_cpg_methylation_level", "Panel_4X_CpG_methylation_level", 1, 25, 0, "number_0.0001", 0],
			  ["panel_ontarget_rate_region_field", "Panel_Ontarget_Rate(region)", 1, 26, 0, "number_0.0001", 0],
			  ["panel_duplication_rate_region_field", "Panel_Duplication_Rate(region)", 1, 27, 0, "number_0.0001", 0],
			  ["panel_depth_site_x_field", "Panel_Depth(site,X)", 1, 28, 0, "number_0.001", 0],
			  ["panel_dedupped_depth_site_x_field", "Panel_Dedupped_Depth(site,X)", 1, 29, 0, "number_0.001", 0],
			  ["panel_coverage_site_1x_field", "Panel_Coverage(site,1X)", 1, 30, 0, "number_0.0001", 0],
			  ["panel_coverage_site_10x_field", "Panel_Coverage(site,10X)", 1, 31, 0, "number_0.0001", 0],
			  ["panel_coverage_site_20x_field", "Panel_Coverage(site,20X)", 1, 32, 0, "number_0.0001", 0],
			  ["panel_coverage_site_50x_field", "Panel_Coverage(site,50X)", 1, 33, 0, "number_0.0001", 0],
			  ["panel_coverage_site_100x_field", "Panel_Coverage(site,100X)", 1, 34, 0, "number_0.0001", 0],
			  ["panel_uniformity_site_20_mean_field", "Panel_Uniformity(site,>20%mean)", 1, 35, 0, "number_0.0001", 0],
			  ["strand_balance_f_field", "Strand_balance(F)", 1, 36, 0, "number_0.0001", 0],
			  ["strand_balance_r_field", "Strand_balance(R)", 1, 37, 0, "number_0.0001", 0],
			  ["gc_bin_depth_ratio", "GC_bin_depth_ratio", 1, 38, 0, "number_0.0001", 0],
              ["sampler_id", "华大编号", 0],
			  ["singleLB_Pooling_id", "测序文库编号", 1, 0, 1, "text"],
			  ["sequencing_id", "上机文库号", 1, 1, 1, "text"],
			  ["memo", "备注", 1, 39, 0, "text", "无"],
              ["last_modify_time", "上次修改时间", 0],
              ["create_time", "创建时间", 0]
          ],
          "update_func": update_func,
          "delete_func": simple_delete_func,
          "bulkDelete_func": simple_bulkDelete_func,
          "download_excel": '/ADVANCE/download_excel/SEQ_' + model0 + '/'
      };
      single_page_generator(model0, url0, id0, bulk_update_bool, bulk_delete_bool, input_params);

  });
</script>
{% endblock %}
